<template>
    <div>
        <h2>科室列表</h2>
        <div>
            科室名称：<input type="text" v-model="info.kename">
            时间：<input type="date" v-model="info.option">-<input type="date" v-model="info.endtime">
            <input type="button" value="查询" class="btn btn-primary" @click="getkeroom"></input>
            <input type="button" value="重置" class="btn btn-primary" @click="rest"></input>
        </div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                   <td>Id</td>
                   <td>科室名称</td>
                   <td>科室人数</td>
                   <td>科室介绍</td>
                   <td>创建时间</td>
                   <td>操作</td>
                </tr>
                 <tr v-for="item in data">
                   <td>{{ item.Id }}</td>
                   <td>{{ item.KeName }}</td>
                   <td>{{ item.KeNum }}</td>
                   <td>{{ item.KeContent }}</td>
                   <td>{{ moment(item.CreateTime).format("YYYY-MM-DD") }}</td>
                   <td>
                    <input type="button" value="编辑" class="btn btn-primary" @click="upt(item.Id)"></input>
                    <input type="button" value="删除" class="btn btn-danger" @click="del(item.Id)"></input>
                   </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from "axios";
import moment from "moment";
import {ref,reactive,onMounted} from "vue"
import {useRoute,useRouter} from "vue-router"
import {useStore} from "@/stores/user"

const route = useRoute();
const router = useRouter();
const abc = useStore();


let info:any = reactive({
    kename:'',    
    opentime:'',
    endtime:''    
})

let data:any = ref([])

onMounted(()=>{
    if(abc.userinfo.Id){
        getkeroom();
    }
    else{
        router.push('/login')
    }
})

//科室信息
const getkeroom=()=>{
    axios({
        url:'/api/YeHotels/GetKeRoomList',
        method:'get',
        params:info
    })
    .then(res=>{
        console.log(res);
        data.value = res.data
    })
    .catch(err=>{
        console.log(err);
        
    })
}


const del=(Id:any)=>{
    var f = confirm("您确定要删除吗")
    if(f){
    axios({
        url:'/api/YeHotels/Del',
        method:'delete',
        params:{
            ids:Id.toString(),
        }
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            getkeroom();
            return;
        }
        else{
            alert('删除失败');
            return;
        }
    })
    .catch(err=>{
        console.log(err);
        
    })
    }
    
}

const upt=(Id:any)=>{
    router.push({
        path:'/updateKeRoom',
        query:{
            id:Id
        }
    })
}

//重置
const rest=()=>{
    info.kename=''
    info.opentime = ''
    info.endtime = ''
}
</script>

<style scoped>
input{
    margin: 0 10px;
}
</style>